// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   GSMAlphabet.java

package cn.sendsms;

import java.util.BitSet;

class GSMAlphabet
{

	private static final char grcAlphabetRemapping[][] = {
		{
			'\u0386', 'A'
		}, {
			'\u0388', 'E'
		}, {
			'\u0389', 'H'
		}, {
			'\u038A', 'I'
		}, {
			'\u038C', 'O'
		}, {
			'\u038E', 'Y'
		}, {
			'\u038F', '\u03A9'
		}, {
			'\u0390', 'I'
		}, {
			'\u0391', 'A'
		}, {
			'\u0392', 'B'
		}, {
			'\u0393', '\u0393'
		}, {
			'\u0394', '\u0394'
		}, {
			'\u0395', 'E'
		}, {
			'\u0396', 'Z'
		}, {
			'\u0397', 'H'
		}, {
			'\u0398', '\u0398'
		}, {
			'\u0399', 'I'
		}, {
			'\u039A', 'K'
		}, {
			'\u039B', '\u039B'
		}, {
			'\u039C', 'M'
		}, {
			'\u039D', 'N'
		}, {
			'\u039E', '\u039E'
		}, {
			'\u039F', 'O'
		}, {
			'\u03A0', '\u03A0'
		}, {
			'\u03A1', 'P'
		}, {
			'\u03A3', '\u03A3'
		}, {
			'\u03A4', 'T'
		}, {
			'\u03A5', 'Y'
		}, {
			'\u03A6', '\u03A6'
		}, {
			'\u03A7', 'X'
		}, {
			'\u03A8', '\u03A8'
		}, {
			'\u03A9', '\u03A9'
		}, {
			'\u03AA', 'I'
		}, {
			'\u03AB', 'Y'
		}, {
			'\u03AC', 'A'
		}, {
			'\u03AD', 'E'
		}, {
			'\u03AE', 'H'
		}, {
			'\u03AF', 'I'
		}, {
			'\u03B0', 'Y'
		}, {
			'\u03B1', 'A'
		}, {
			'\u03B2', 'B'
		}, {
			'\u03B3', '\u0393'
		}, {
			'\u03B4', '\u0394'
		}, {
			'\u03B5', 'E'
		}, {
			'\u03B6', 'Z'
		}, {
			'\u03B7', 'H'
		}, {
			'\u03B8', '\u0398'
		}, {
			'\u03B9', 'I'
		}, {
			'\u03BA', 'K'
		}, {
			'\u03BB', '\u039B'
		}, {
			'\u03BC', 'M'
		}, {
			'\u03BD', 'N'
		}, {
			'\u03BE', '\u039E'
		}, {
			'\u03BF', 'O'
		}, {
			'\u03C0', '\u03A0'
		}, {
			'\u03C1', 'P'
		}, {
			'\u03C2', '\u03A3'
		}, {
			'\u03C3', '\u03A3'
		}, {
			'\u03C4', 'T'
		}, {
			'\u03C5', 'Y'
		}, {
			'\u03C6', '\u03A6'
		}, {
			'\u03C7', 'X'
		}, {
			'\u03C8', '\u03A8'
		}, {
			'\u03C9', '\u03A9'
		}, {
			'\u03CA', 'I'
		}, {
			'\u03CB', 'Y'
		}, {
			'\u03CC', 'O'
		}, {
			'\u03CD', 'Y'
		}, {
			'\u03CE', '\u03A9'
		}
	};
	private static final char extAlphabet[] = {
		'\f', '^', '{', '}', '\\', '[', '~', ']', '|', '\u20AC'
	};
	private static final String extBytes[] = {
		"1b0a", "1b14", "1b28", "1b29", "1b2f", "1b3c", "1b3d", "1b3e", "1b40", "1b65"
	};
	private static final char stdAlphabet[] = {
		'@', '\243', '$', '\245', '\350', '\351', '\371', '\354', '\362', '\347', 
		'\n', '\330', '\370', '\r', '\305', '\345', '\u0394', '_', '\u03A6', '\u0393', 
		'\u039B', '\u03A9', '\u03A0', '\u03A8', '\u03A3', '\u0398', '\u039E', '\240', '\306', '\346', 
		'\337', '\311', ' ', '!', '"', '#', '\244', '%', '&', '\'', 
		'(', ')', '*', '+', ',', '-', '.', '/', '0', '1', 
		'2', '3', '4', '5', '6', '7', '8', '9', ':', ';', 
		'<', '=', '>', '?', '\241', 'A', 'B', 'C', 'D', 'E', 
		'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 
		'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 
		'Z', '\304', '\326', '\321', '\334', '\247', '\277', 'a', 'b', 'c', 
		'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 
		'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 
		'x', 'y', 'z', '\344', '\366', '\361', '\374', '\340'
	};

	GSMAlphabet()
	{
	}

	static String bytesToString(byte bytes[])
	{
		StringBuffer text = new StringBuffer();
label0:
		for (int i = 0; i < bytes.length; i++)
		{
			if (bytes[i] == 27)
			{
				String extChar = "1b" + Integer.toHexString(bytes[++i]);
				int j = 0;
				do
				{
					if (j >= extBytes.length)
						continue label0;
					if (extBytes[j].equalsIgnoreCase(extChar))
						text.append(extAlphabet[j]);
					j++;
				} while (true);
			}
			text.append(stdAlphabet[bytes[i]]);
		}

		return text.toString();
	}

	static int stringToBytes(String text, byte bytes[])
	{
		int k = 0;
label0:
		for (int i = 0; i < text.length(); i++)
		{
			char ch = text.charAt(i);
			int index = -1;
			int j = 0;
			do
			{
				if (j >= extAlphabet.length)
					break;
				if (extAlphabet[j] == ch)
				{
					index = j;
					break;
				}
				j++;
			} while (true);
			if (index != -1)
			{
				bytes[k] = (byte)Integer.parseInt(extBytes[index].substring(0, 2), 16);
				k++;
				bytes[k] = (byte)Integer.parseInt(extBytes[index].substring(2, 4), 16);
				k++;
				continue;
			}
			index = -1;
			j = 0;
			do
			{
				if (j >= stdAlphabet.length)
					break;
				if (stdAlphabet[j] == ch)
				{
					index = j;
					bytes[k] = (byte)j;
					k++;
					break;
				}
				j++;
			} while (true);
			if (index != -1)
				continue;
			j = 0;
			do
			{
				if (j >= grcAlphabetRemapping.length)
					break;
				if (grcAlphabetRemapping[j][0] == ch)
				{
					index = j;
					ch = grcAlphabetRemapping[j][1];
					break;
				}
				j++;
			} while (true);
			if (index != -1)
			{
				j = 0;
				do
				{
					if (j >= stdAlphabet.length)
						continue label0;
					if (stdAlphabet[j] == ch)
					{
						index = j;
						bytes[k] = (byte)j;
						k++;
						continue label0;
					}
					j++;
				} while (true);
			}
			bytes[k] = 32;
			k++;
		}

		return k;
	}

	static String textToPDU(String txt)
	{
		String pdu = "";
		byte txtBytes[] = new byte[txt.length() * 2];
		int txtBytesLen = stringToBytes(txt, txtBytes);
		BitSet bits = new BitSet();
		for (int i = 0; i < txtBytesLen; i++)
		{
			for (int j = 0; j < 7; j++)
				if ((txtBytes[i] & 1 << j) != 0)
					bits.set(i * 7 + j);

		}

		int txtSeptetsLen = (int)Math.ceil((double)(txtBytesLen * 7) / 8D);
		short txtSeptets[] = new short[txtSeptetsLen];
		for (int i = 0; i < txtSeptetsLen; i++)
		{
			for (int j = 0; j < 8; j++)
				txtSeptets[i] |= (short)((bits.get(i * 8 + j) ? 1 : 0) << j);

		}

		for (int i = 0; i < txtSeptetsLen; i++)
		{
			String c = Integer.toHexString(txtSeptets[i]);
			if (c.length() < 2)
				c = "0" + c;
			pdu = pdu + c;
		}

		return pdu;
	}

}
